package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.MemberReturnVisitorClient;
import com.scada.model.bean.MemberReturnVisit;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.MemberReturnVisitVO;
import com.scada.model.dataTable.MemberReturnVisitList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MemberReturnVisitSearch;

@Controller
@RequestMapping("memberReturnVisit")
public class MemberReturnVisitController extends BaseController{

	@Autowired
	private MemberReturnVisitorClient memberReturnVisitorClient;
	
	
	@RequestMapping(value = "toReturnVisitPage")
	public String toReturnVisitPage(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return "member/returnVisit/list";
	}
	
	@RequestMapping(value = "getReturnVisitPageDate")
	@ResponseBody
	public String getReturnVisitPageDate(@ModelAttribute MemberReturnVisitSearch memberReturnVisitSearch){
		
		PageInfo<MemberReturnVisitList> pageInfo = new PageInfo<MemberReturnVisitList>();
        pageInfo.setStart(memberReturnVisitSearch.getStart());
        pageInfo.setPageSize(memberReturnVisitSearch.getLength());
        pageInfo.setDraw(memberReturnVisitSearch.getDraw());
        ResponseBean rb = memberReturnVisitorClient.getMemberReturnVisitPageData(memberReturnVisitSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberReturnVisitList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberReturnVisitList>());
        }
        
		return toJson(pageInfo);
	}
	
	
	@RequestMapping(value = "toSaveReturnVisitFrom")
	public String toSaveReturnVisitFrom(Long memberId, ModelMap model){
		model.put("memberId", memberId);
		return "member/returnVisit/add";
	}
	
	@RequestMapping(value = "insertMemberReturnVisit")
	@ResponseBody
	public String insertMemberReturnVisit(@ModelAttribute MemberReturnVisit memberReturnVisit) {
		
		ResponseBean rb = new ResponseBean();
		try{
			User user = getCurrentUser();
			memberReturnVisit.setCreateId(user.getId());
			memberReturnVisit.setLastUpdateId(user.getId());
			memberReturnVisitorClient.insertMemberReturnVisit(memberReturnVisit);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		
		return toJson(rb);
	}
	
	@RequestMapping(value = "toEditReturnVisitFrom")
	public ModelAndView toEditReturnVisitFrom(Long id, ModelMap model){
		
		MemberReturnVisitVO mrv = null;
		try {
			ResponseBean rb = memberReturnVisitorClient.getMemberReturnVisitById(id);
			if(StringUtils.equals("2", rb.getType())){
				mrv = (MemberReturnVisitVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		
		model.put("mrv", mrv);
		return new ModelAndView("member/returnVisit/edit");
	}
	
	@RequestMapping(value = "updateMemberReturnVisit")
	@ResponseBody
	public String updateMemberReturnVisit(@ModelAttribute MemberReturnVisit memberReturnVisit) {
		
		ResponseBean rb = new ResponseBean();
		try{
			User user = getCurrentUser();
			memberReturnVisit.setLastUpdateId(user.getId());
			memberReturnVisitorClient.updateMemberReturnVisit(memberReturnVisit);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		
		return toJson(rb);
	}
	
	@RequestMapping(value = "deleteReturnVisitById")
	@ResponseBody
	public String deleteReturnVisitById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			memberReturnVisitorClient.deleteMemberReturnVisitById(MemberReturnVisit.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
}
